package cn.antho.org.service.impl;

import cn.antho.basic.query.BaseQuery;
import cn.antho.basic.service.impl.BaseServiceImpl;
import cn.antho.basic.util.LoginContext;
import cn.antho.basic.util.MD5Utils;
import cn.antho.basic.util.PageList;
import cn.antho.basic.util.StrUtils;
import cn.antho.org.domain.Employee;
import cn.antho.org.mapper.EmployeeMapper;
import cn.antho.org.query.EmployeeQuery;
import cn.antho.org.service.IEmployeeService;
import cn.antho.system.mapper.RoleMapper;
import cn.antho.user.domain.Logininfo;
import cn.antho.user.mapper.LogininfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Override
    @Transactional
    public void add(Employee employee) {
        // 设置默认密码
        if (null == employee.getPassword()) {
            employee.setPassword("123456");
        }
        // 加密密码
        String salt = StrUtils.getComplexRandomString(32);
        String encrypPassword = MD5Utils.encrypByMd5(salt + employee.getPassword());
        employee.setSalt(salt);
        employee.setPassword(encrypPassword);
        // 同步到logininfo
        Logininfo logininfo = empToLogininfo(employee);
        logininfoMapper.add(logininfo);
        employee.setLogininfoId(logininfo.getId());
        employeeMapper.add(employee);
        System.out.println(employee);
        // 添加角色
        roleMapper.addMiddle(employee.getId(), employee.getRoles());
    }

    @Override
    public Employee findByLogininfoId(Long id) {
        return employeeMapper.findByLogininfoId(id);
    }

    @Override
    public List<Employee> queryByShopId(Long shopId) {
        List<Employee> emps = employeeMapper.selectByShopId(shopId);
        return emps;
    }

    private Logininfo empToLogininfo(Employee employee) {
        return new Logininfo().builder()
                .username(employee.getUsername())
                .phone(employee.getPhone())
                .salt(employee.getSalt())
                .password(employee.getPassword())
                .email(employee.getEmail())
                .type(1)
                .disable(true)
                .build();
    }

    @Override
    @Transactional
    public void update(Employee employee) {
        super.update(employee);
        Logininfo logininfo = empToLogininfo(employee);
        logininfoMapper.update(logininfo);
        // 删除角色
        roleMapper.delMiddle(employee.getId());
        // 新增角色
        roleMapper.addMiddle(employee.getId(), employee.getRoles());
    }

    @Override
    @Transactional
    public void delete(Long id) {
        // 删除logininfo
        logininfoMapper.delete(employeeMapper.findById(id).getLogininfoId());
        super.delete(id);
        // 删除角色
        roleMapper.delMiddle(id);
    }

    @Override
    public PageList<Employee> queryByPage(BaseQuery query) {
        PageList<Employee> pageList = super.queryByPage(query);
        List<Employee> emps = pageList.getRows();
        emps.forEach(e->{
            List<Long> roles = roleMapper.findRoles(e.getId());
            e.setRoles(roles);
        });
        return pageList;
    }
}
